import { CHANGE_INPUT_VALUE, ADD_TO_ITEM, DELETE_TODO_ITEM, INIT_LIST_ACTION } from './actionType';

const defaultSate = {
  inputValue: '',
  list: []
}


// reducer 可以接受state，但是不能修改state
// 纯函数指的是，给固定的输入，就一定会有固定的输出，而且不会有任何副作用
export default (state = defaultSate, action) => {
  if (action.type === CHANGE_INPUT_VALUE) {
    const newState = JSON.parse(JSON.stringify(state));
    newState.inputValue = action.value;
    return newState;
  }
  if (action.type === INIT_LIST_ACTION) {
    const newState = JSON.parse(JSON.stringify(state));
    newState.list = action.data;
    return newState;
  }
  if (action.type === ADD_TO_ITEM) {
    const newState = JSON.parse(JSON.stringify(state));
    newState.list.push(newState.inputValue);
    newState.inputValue = '';
    return newState;
  }
  if (action.type === DELETE_TODO_ITEM) {
    const newState = JSON.parse(JSON.stringify(state));
    newState.list.splice(action.index, 1);
    return newState;
  }
  return state;
}